﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Studio
{
	// Token: 0x020004C2 RID: 1218
	public class GuideRotation : GuideBase, IInitializePotentialDragHandler, IPointerDownHandler, IPointerUpHandler, IEventSystemHandler
	{
		// Token: 0x17000385 RID: 901
		// (get) Token: 0x06001F0F RID: 7951 RVA: 0x000C1DB0 File Offset: 0x000BFFB0
		private Camera camera
		{
			get
			{
				if (this.m_Camera == null)
				{
					this.m_Camera = Camera.main;
				}
				return this.m_Camera;
			}
		}

		// Token: 0x06001F10 RID: 7952 RVA: 0x000C1DD4 File Offset: 0x000BFFD4
		public void OnPointerDown(PointerEventData eventData)
		{
		}

		// Token: 0x06001F11 RID: 7953 RVA: 0x000C1DD8 File Offset: 0x000BFFD8
		public void OnInitializePotentialDrag(PointerEventData _eventData)
		{
			this.prevScreenPos = _eventData.position;
			this.prevPlanePos = this.PlanePos(_eventData.position);
			this.dicChangeAmount = Singleton<GuideObjectManager>.Instance.selectObjectDictionary;
			this.dicOldRot = this.dicChangeAmount.ToDictionary((KeyValuePair<int, ChangeAmount> v) => v.Key, (KeyValuePair<int, ChangeAmount> v) => v.Value.rot);
		}

		// Token: 0x06001F12 RID: 7954 RVA: 0x000C1E60 File Offset: 0x000C0060
		public override void OnDrag(PointerEventData _eventData)
		{
			base.OnDrag(_eventData);
			Vector3 zero = Vector3.zero;
			float f = Vector3.Dot(this.camera.transform.forward, base.transform.right);
			if (Mathf.Abs(f) > 0.1f)
			{
				Vector3 position = this.PlanePos(_eventData.position);
				Vector3 vector = Quaternion.Euler(0f, 90f, 0f) * base.transform.InverseTransformPoint(this.prevPlanePos);
				Vector3 vector2 = Quaternion.Euler(0f, 90f, 0f) * base.transform.InverseTransformPoint(position);
				float value = this.VectorToAngle(new Vector2(vector.x, vector.y), new Vector2(vector2.x, vector2.y));
				zero[(int)this.axis] = value;
				this.prevPlanePos = position;
			}
			else
			{
				Vector3 position2 = _eventData.position;
				position2.z = Vector3.Distance(this.prevPlanePos, this.camera.transform.position);
				Vector3 position3 = this.prevScreenPos;
				position3.z = Vector3.Distance(this.prevPlanePos, this.camera.transform.position);
				Vector3 b = this.camera.ScreenToWorldPoint(position2) - this.camera.ScreenToWorldPoint(position3);
				Vector3 position4 = this.prevPlanePos + b;
				Vector3 vector3 = Quaternion.Euler(0f, 90f, 0f) * base.transform.InverseTransformPoint(this.prevPlanePos);
				Vector3 vector4 = Quaternion.Euler(0f, 90f, 0f) * base.transform.InverseTransformPoint(position4);
				this.prevPlanePos = position4;
				float value2 = this.VectorToAngle(new Vector2(vector3.x, vector3.y), new Vector2(vector4.x, vector4.y));
				zero[(int)this.axis] = value2;
				this.prevPlanePos = position4;
			}
			this.prevScreenPos = _eventData.position;
			foreach (KeyValuePair<int, ChangeAmount> keyValuePair in this.dicChangeAmount)
			{
				Vector3 eulerAngles = (Quaternion.Euler(keyValuePair.Value.rot) * Quaternion.Euler(zero)).eulerAngles;
				eulerAngles.x %= 360f;
				eulerAngles.y %= 360f;
				eulerAngles.z %= 360f;
				keyValuePair.Value.rot = eulerAngles;
			}
		}

		// Token: 0x06001F13 RID: 7955 RVA: 0x000C214C File Offset: 0x000C034C
		public void OnPointerUp(PointerEventData _eventData)
		{
		}

		// Token: 0x06001F14 RID: 7956 RVA: 0x000C2150 File Offset: 0x000C0350
		public override void OnEndDrag(PointerEventData _eventData)
		{
			base.OnEndDrag(_eventData);
			GuideCommand.EqualsInfo[] changeAmountInfo = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select new GuideCommand.EqualsInfo
			{
				dicKey = v,
				oldValue = this.dicOldRot[v],
				newValue = this.dicChangeAmount[v].rot
			}).ToArray<GuideCommand.EqualsInfo>();
			Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.RotationEqualsCommand(changeAmountInfo));
		}

		// Token: 0x06001F15 RID: 7957 RVA: 0x000C2198 File Offset: 0x000C0398
		private Vector3 PlanePos(Vector2 _screenPos)
		{
			Plane plane = new Plane(base.transform.right, base.transform.position);
			if (!plane.GetSide(this.camera.transform.position))
			{
				plane.SetNormalAndPosition(base.transform.right * -1f, base.transform.position);
			}
			Ray ray = RectTransformUtility.ScreenPointToRay(this.camera, _screenPos);
			float distance = 0f;
			return (!plane.Raycast(ray, out distance)) ? base.transform.position : ray.GetPoint(distance);
		}

		// Token: 0x06001F16 RID: 7958 RVA: 0x000C2240 File Offset: 0x000C0440
		private float VectorToAngle(Vector2 _v1, Vector2 _v2)
		{
			float current = Mathf.Atan2(_v1.x, _v1.y) * 57.29578f;
			float target = Mathf.Atan2(_v2.x, _v2.y) * 57.29578f;
			return Mathf.DeltaAngle(current, target);
		}

		// Token: 0x040020EB RID: 8427
		public GuideRotation.RotationAxis axis;

		// Token: 0x040020EC RID: 8428
		private Vector2 prevScreenPos = Vector2.zero;

		// Token: 0x040020ED RID: 8429
		private Vector3 prevPlanePos = Vector3.zero;

		// Token: 0x040020EE RID: 8430
		private Dictionary<int, Vector3> dicOldRot;

		// Token: 0x040020EF RID: 8431
		private Dictionary<int, ChangeAmount> dicChangeAmount;

		// Token: 0x040020F0 RID: 8432
		private Camera m_Camera;

		// Token: 0x020004C3 RID: 1219
		public enum RotationAxis
		{
			// Token: 0x040020F4 RID: 8436
			X,
			// Token: 0x040020F5 RID: 8437
			Y,
			// Token: 0x040020F6 RID: 8438
			Z
		}
	}
}
